﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SearchClient.ServiceClientReference;

namespace SearchClient
{
    public partial class Login : UserControl
    {
        private ServiceClientClient service = new ServiceClientClient();

        public Login()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            lblErrores.Visibility = Visibility.Collapsed;
            service.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(LoginCompleted_EventHandler);
            try
            {
                string usuario = txtUsuario.Text;
                string password = txtPassword.Password;
                if (string.IsNullOrEmpty(usuario) || string.IsNullOrEmpty(password))
                {
                    lblErrores.Visibility = Visibility.Visible;
                    lblErrores.Content = "Se deben llenar ambos campos para iniciar sesión.";
                }
                else
                {
                    service.LoginAsync(usuario, password);
                }
            }
            catch (Exception ex)
            {
                lblErrores.Visibility = Visibility.Visible;
                lblErrores.Content = ex.Message;
            }
        }

        internal void LoginCompleted_EventHandler(object sender, LoginCompletedEventArgs e)
        {
            try
            {
                Usuario usuario = e.Result;
                bool tienePermisos = false;


                foreach (string funcionalidad in usuario.Funcionalidades)
                {
                    if (funcionalidad.ToUpper().Equals("BUSQUEDASILVERLIGHT"))
                        tienePermisos = true;
                }

                if (tienePermisos)
                {
                    Grid root = Application.Current.RootVisual as Grid;
                    if (root != null)
                    {
                        root.Children.RemoveAt(0);
                        root.Children.Add(new MainPage(usuario));
                    }
                }
                else
                {
                    txtUsuario.Text = string.Empty;
                    txtPassword.Password = string.Empty;
                    lblErrores.Visibility = Visibility.Visible;
                    lblErrores.Content = "El usuario identificado no tiene los permisos necesarios.";
                }
            }
            catch (Exception ex)
            {
                txtUsuario.Text = string.Empty;
                txtPassword.Password = string.Empty;
                lblErrores.Visibility = Visibility.Visible;
                lblErrores.Content = ex.Message;
            }
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                btnLogin_Click(sender, e);
        }
    }
}
